package com.onepower.core.modules.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onepower.core.domain.response.MatchProjectScoreInfo;
import com.onepower.core.exception.BizException;
import com.onepower.core.modules.repository.entity.MatchProjectScore;
import com.onepower.core.modules.repository.mapper.MatchProjectScoreMapper;
import com.onepower.core.modules.service.IMatchProjectScoreService;
import com.onepower.core.result.BizResultCode;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author kin
 * @since 2018-08-24
 */
@Service
public class MatchProjectScoreServiceImpl extends ServiceImpl<MatchProjectScoreMapper, MatchProjectScore> implements IMatchProjectScoreService {


    @Override
    public Page<MatchProjectScoreInfo> getList(Page<MatchProjectScoreInfo> page, Integer matchScheduleId, String referee) {
        List<MatchProjectScoreInfo> matchProjectScoreList= baseMapper.selectProjectScoreList(page, matchScheduleId, referee);
        return  page.setRecords(matchProjectScoreList);
    }

    @Override
    public void saveProjectScore(List<MatchProjectScore> matchProjectScoreList) {
        try {
            if (CollectionUtils.isNotEmpty(matchProjectScoreList)) {
                if(matchProjectScoreList.size()==1){
                    this.save(matchProjectScoreList.stream().findAny().get());
                }
                else {
                    this.saveBatch(matchProjectScoreList);
                }
            }
        } catch (DuplicateKeyException e) {
            throw new BizException(BizResultCode.MATCH_SCORE_HAS_JUDGE);
        }
    }
}
